<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img />
      </div>
      <!-- 表单区 引用可以用ref-->
      <el-form
        hide-required-asterisk
        label-width="55px"
        ref="loginFormRef"
        :model="loginForm"
        class="login_form"
      >
        <!-- 用户名  prop指定验证对象-->
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            show-password
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button @click="login" type="primary">登录</el-button>
          <el-button @click="resetLoginForm" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userLoginAPI } from "@/apis/login";
import { mapMutations } from "vuex";
import { USERNAME, PASSWORD } from "@/configs/account";
export default {
  name: "loginPage",
  data() {
    return {
      // 登录表单绑定对象
      loginForm: {
        username: "",
        password: "",
      },
    };
  },
  created() {
    this.loginForm.username = USERNAME;
    this.loginForm.password = PASSWORD;
  },
  methods: {
    // 点击按钮重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 登录
    async login() {
      const { username, password } = this.loginForm;
      const data = await userLoginAPI(username, password);
      if (data.code === "200" && data.data.isAdmin === 1) {
        const token = data.data;
        this.setToken(token.tokenHead + " " + token.token);
        this.$router.push("/home");
        this.$message.success("登录成功");
      } else {
        this.$message.error("信息错误");
      }
    },
    ...mapMutations("login", ["setToken"]),
  },
};
</script>
<style lang="less" scoped>
.login_container {
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../../assets/image/login_bg.jpg");
  background-size: cover;
  height: 100vh;
  width: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #eee;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  opacity: 0.9;
  transform: translate(-50%, -50%);
}

.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  user-select: none;
  /* 添加该样式以禁用选中 */

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-image: url("../../assets/image/login_av.png");
    background-size: 140%;
    background-repeat: no-repeat;
    background-position: 50%;
  }
}

.login_form {
  position: absolute;
  // background-color:red;
  top: 100px;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
